<!--
 * @Author: lxy 2110251760@qq.com
 * @Date: 2022-10-12 10:45:06
 * @LastEditors: lxy 2110251760@qq.com
 * @LastEditTime: 2022-10-17 18:53:49
 * @FilePath: \jep-training-trinity-group\briup-ui\src\pages\admin\custome\List.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <!-- 顾客管理 -->
  <div>
    <!-- 第一行开始 添加 查询 -->
    <div class="header">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item>
          <el-button type="primary" @click="onAdd">添加</el-button>
        </el-form-item>

        <el-form-item>
          <el-input
            v-model="formInline.username"
            clearable
            placeholder="请输入用户名"
          />
        </el-form-item>

        <el-form-item>
          <el-select
            v-model="formInline.status"
            clearable
            placeholder="请选择状态"
          >
            <el-option label="禁用" value="禁用" />
            <el-option label="正常" value="正常" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 第一行结束 -->

    <!--第二行 表格开始 -->
    <!-- {{ formInline }} -->
    <el-table v-loading="loading" :data="userData" style="width: 100%">
      <el-table-column prop="id" label="编号" width="180" />

      <el-table-column prop="username" label="用户名" />

      <el-table-column prop="realname" label="姓名" width="180" />

      <el-table-column prop="telephone" label="手机号" width="180" />

      <el-table-column prop="gender" label="性别">
        <template slot-scope="scope">
          <!-- {{scope.row}} -->

          <i v-if="scope.row.gender == 'male'" class="el-icon-male" />
          <i
            v-else-if="scope.row.gender == 'female'"
            class="el-icon-female"
          />

          <a v-else>暂无</a>
        </template>
      </el-table-column>

      <el-table-column prop="status" label="状态" width="180">
        <template slot-scope="scope">
          <!-- {{scope.row.status}} -->
          <el-tag
            v-if="scope.row.status === '正常'"
            type="success"
          >正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <a href="#" class="toEdit" @click="toEdit(scope.row)">编辑</a>
          <a
            href="#"
            class="offLine"
            @click.prevent="toDelete(scope.row.id)"
          >删除</a>
          <a
            href="#"
            class="onLine"
            @click.prevent="toDetails(scope.row.id)"
          >详情</a>
        </template>
      </el-table-column>
    </el-table>
    <!---第二行  表格结束 -->

    <!-- 第三行 分页开始 -->
    <div class="page">
      <el-pagination
        :current-page="formInline.page"
        :page-sizes="[2, 5, 10, 20, 30, 50]"
        :page-size="formInline.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 第三行 分页结束 -->

    <!-- 第四部分 添加按钮和编辑按钮触发的模态框 -->
    <!-- close 表示模态框关闭的回调 组件库提供的方法 -->
    <el-dialog :title="title" :visible.sync="visible" @close="toClose">
      <el-form ref="myForm" :model="form" :rules="rules">
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>

        <el-form-item
          label="姓名"
          :label-width="formLabelWidth"
          prop="realname"
        >
          <el-input v-model="form.realname" autocomplete="off" />
        </el-form-item>

        <el-form-item
          label="手机号"
          :label-width="formLabelWidth"
          prop="telephone"
        >
          <el-input v-model="form.telephone" autocomplete="off" />
        </el-form-item>

        <el-form-item
          label="密码"
          :label-width="formLabelWidth"
          prop="password"
        >
          <el-input
            v-model="form.password"
            autocomplete="off"
            type="password"
          />
        </el-form-item>

        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-radio v-model="form.gender" label="male">男</el-radio>
          <el-radio v-model="form.gender" label="female">女</el-radio>
        </el-form-item>

        <el-form-item v-if="title==&quot;编辑顾客信息&quot;" label="状态" :label-width="formLabelWidth" prop="status">
          <el-radio v-model="form.status" label="禁用">禁用</el-radio>
          <el-radio v-model="form.status" label="正常">正常</el-radio>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="toClose()">取消</el-button>
        <el-button type="primary" @click="toSubmit">确定</el-button>
      </div>
    </el-dialog>
    <!-- 第四部分 添加按钮结束  -->
  </div>
</template>

<script>
import {
  pageQuery,
  saveOrUpdate,
  addUserWidthRole,
  deleteById
} from '../../../api/user'
export default {
  data() {
    return {
      // 表单对象和分页数据
      formInline: {
        page: 1,
        pageSize: 10,
        roleId: 6
      },
      // 加载中的动画
      loading: false,
      // 表格数据来源 顾客数据
      userData: [],
      total: 0,
      // 控制模态框显示与隐藏
      visible: false,
      // 模态框标题
      title: '',
      // 表单对象
      form: {},
      formLabelWidth: '120px',

      // 表单校验规则
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        realname: [
          {
            required: true,
            message: '请输入姓名',
            trigger: 'blur'
          }
        ],
        telephone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  created() {
    this.getUser()
  },
  mounted() {},
  methods: {

    // 点击详情
    toDetails(id) {
      this.$router.push({ path: './Details', query: { id }})
    },
    // 删除方法
    toDelete(id) {
      //  提示用户
      this.$confirm('此操作将删除该顾客, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteById({ id })
        console.log(res, '删除顾客')
        this.$message({
          type: 'success',
          message: res.message
        })
        this.getUser()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑方法
    toEdit(row) {
      this.title = '编辑顾客信息'
      this.visible = true
      this.form = { ...row }
    },
    // 确定按钮 保存或更新
    toSubmit() {
      if (this.title === '添加顾客信息') {
        // 1,校验表单
        this.$refs.myForm.validate(async(valid) => {
          if (valid) {
            // 发送保存或者更新请求
            // 去api封装保存或更新的异步请求
            const resId = { ...this.form, roleId: 6 }
            const res = await addUserWidthRole(resId)
            this.$notify({
              type: 'success',
              message: res.message
            })
            this.getUser()
            this.toClose() // 重置表单验证  清空表单验证
          } else {
            return false
          }
        })
      } else {
        // 1,校验表单
        this.$refs.myForm.validate(async(valid) => {
          if (valid) {
            // 发送保存或者更新请求
            // 去api封装保存或更新的异步请求
            const res = await saveOrUpdate(this.form)
            this.$notify({
              type: 'success',
              message: res.message
            })
            this.getUser()
            this.toClose() // 重置表单验证  清空表单验证
          } else {
            return false
          }
        })
      }
    },
    // 模态框关闭的方法
    toClose() {
      // 重置表单验证 清空表单验证
      setTimeout(() => {
        this.$refs.myForm.resetFields()
        this.visible = false
      }, 500)
    },
    // 当前页码发生改变
    handleCurrentChange(page) {
      this.formInline.page = page
      this.getUser()
    },
    // 页码条数发送改变
    handleSizeChange(pageSize) {
      this.formInline.pageSize = pageSize
      this.formInline.page = 1
      this.getUser()
    },
    // 获取所有顾客
    async getUser() {
      // 数据获取到之前加载动画
      this.loading = true
      for (const key in this.formInline) {
        if (!this.formInline[key]) {
          delete this.formInline[key]
        }
      }
      const res = await pageQuery(this.formInline)
      console.log(res, '分页查询顾客信息')
      this.userData = res.data.list
      this.total = res.data.total
      this.loading = false
      this.gender = res.data.gender
    },
    // 点击添加的方法
    onAdd() {
      this.visible = true
      this.title = '添加顾客信息'
      this.form = {}
    },
    // 点击查询的方法
    onSearch() {
      this.formInline.page = 1
      this.getUser()
    }
  }
}
</script>
<style scoped>
.toEdit {
  color: green;
}
.offLine,
.el-icon-female {
  color: red;
}
.onLine,
.el-icon-male {
  color: blue;
}
</style>
